<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\UserAddRequest;


class UserAddController extends Controller
{
    
    public function getIndex()
    {
        //初始化
          //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
      // dd($type);

      //获取配置信息
       $config = DB::table('config')->first();

      //友情链接
       $blinks = DB::table('blink')->get();

      //遍历下部的栏目
      $datas = self::pushCates(self::pushType(),0);

      //查询所有的申请类型
      $cates = DB::table('ask')->get();

      // var_dump($type);
       return view('Home.loveser.useradd',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'cates'=>$cates]);
    }


    //获取全部一级分类板块
    public static function pushType()
    {
        return $cates = DB::table('type')->get();
    }

    
    //获取二级分类
    public  static function pushCates($cates,$pid)
    {
        $data = [];
        foreach($cates as $k=>$v)
        {
            if($v->pid == $pid){

               $v->sub = self::pushCates($cates,$v->id);
               $data[] = $v;
            }
        }
        return $data;
    }

    //志愿者添加
    public function postInsert(UserAddRequest $request)
    {
      // dd($request->all());
        $data = $request->except('_token');

        $data['time']=time();
        $data['status']=0;
        //提取数据
        $res = DB::table('loveperson')->insert($data);

        if($res){

            return redirect('homes/useradd')->with('success','信息提交成功,会尽快联系您');
        }else{

            return back()->with('error','信息提交失败');
        }
    }
    
}
